# encoding=utf-8

import sys
sys.path.append("/home/hadoop/BookSellAnalyies")
from com.utls.pro_env import *
from xml.etree.ElementTree import parse
from com.utls.sqoop import *

def resolve_conf(dt):
    conf_file = PROJECT_CONF_DIR + "Import.xml"

    f = open(conf_file)
    xml_tree = parse(f)
    tasks = xml_tree.findall('./task')

    for task in tasks:
        import_type = task.attrib['type']
        tables = task.findall('./table')
        cmds = []


        for i in range(len(tables)):
            table_name = tables[i].text
            table_conf_file = PROJECT_CONF_DIR + table_name + ".xml"
            f = open(table_conf_file)
            xmlTree = parse(f)
            sqoopNodes = xmlTree.findall("./sqoop-shell")
            sqoop_cmd_type = sqoopNodes[0].attrib["type"]
            praNodes = sqoopNodes[0].findall("./param")
            cmap={}

            for i in range(len(praNodes)):
                key = praNodes[i].attrib["key"]
                value = praNodes[i].text
                cmap[key] = value

                command = "sqoop" + " " + sqoop_cmd_type

                if(import_type == "all"):
                    import_condition = "< " + dt
                elif (import_type == "add"):
                    import_condition = "= " + dt
                else:
                    raise Exception

                for key in cmap.keys():
                    value = cmap[key]
                    if(value == None or value == "" or value==" "):
                        value = ""

                    if(key == "query"):
                        value = value.replace("\$CONDITIONS", import_condition)

                    if(key == "hive-partition-value"):
                        value = value.replace("\$dt", dt)


                    command += " --" + key + " " + value

            cmds.append(command)
    return cmds


if __name__ == '__main__':
    if len(sys.argv) <2:
        print "miss argument"
        exit(1)

    dt = sys.argv[1]
    cmds = resolve_conf(dt)

    for i in range(len(cmds)):
        cmd = cmds[i]
        print cmd

        shell_ret = SqoopUtil.execute_shell(cmd)
        print shell_ret